home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DDJ0192.ARJ / SEARCH.C < prev    next >
Text File  |  1991-09-30  |  3KB  |  135 lines

  1. /* ---------------- search.c ------------- */
  2. #include "dflat.h"
  3.  
  4. extern DBOX SearchTextDB;
  5. extern DBOX ReplaceTextDB;
  6. static int CheckCase = TRUE;
  7.  
  8. static int SearchCmp(int a, int b)
  9. {
  10.     if (b == '\n')
  11.         b = ' ';
  12.     if (CheckCase)
  13.         return a != b;
  14.     return tolower(a) != tolower(b);
  15. }
  16.  
  17. static void SearchTextBox(WINDOW wnd, int Replacing, int incr)
  18. {
  19.     char *s1, *s2, *cp1 = CurrChar;
  20.     DBOX *db = Replacing ? &ReplaceTextDB : &SearchTextDB;
  21.     char *cp = GetEditBoxText(db, ID_SEARCHFOR);
  22.     int rpl = TRUE, FoundOne = FALSE;
  23.     while (rpl)    {
  24.         rpl = Replacing ?
  25.                 CheckBoxSetting(&ReplaceTextDB, ID_REPLACEALL) : FALSE;
  26.         if (TextBlockMarked(wnd))    {
  27.             ClearTextBlock(wnd);
  28.             SendMessage(wnd, PAINT, 0, 0);
  29.         }
  30.         if (cp && cp1 && *cp && *cp1)    {
  31.             if (incr)
  32.                 cp1++;
  33.             while (*cp1)    {
  34.                 s1 = cp;
  35.                 s2 = cp1;
  36.                 while (*s1 && *s1 != '\n')    {
  37.                     if (SearchCmp(*s1,*s2))
  38.                         break;
  39.                     s1++, s2++;
  40.                 }
  41.                 if (*s1 == '\0' || *s1 == '\n')
  42.                     break;
  43.                 cp1++;
  44.             }
  45.             if (*s1 == 0 || *s1 == '\n')    {
  46.                 /* ----- hit at *cp1 ------- */
  47.                 FoundOne = TRUE;
  48.                 wnd->BlkEndLine = TextLineNumber(wnd, s2);
  49.                 wnd->BlkBegLine = TextLineNumber(wnd, cp1);
  50.                 if (wnd->BlkEndLine < wnd->BlkBegLine)
  51.                     wnd->BlkEndLine = wnd->BlkBegLine;
  52.                 wnd->BlkEndCol = (int)(s2 - TextLine(wnd, wnd->BlkEndLine));
  53.                 wnd->BlkBegCol = (int)(cp1 - TextLine(wnd, wnd->BlkBegLine));
  54.  
  55.                 wnd->CurrCol = wnd->BlkBegCol;
  56.                 wnd->CurrLine = wnd->BlkBegLine;
  57.                 wnd->WndRow = wnd->CurrLine - wnd->wtop;
  58.  
  59.                 if (WndCol > ClientWidth(wnd)-1)
  60.                     wnd->wleft = wnd->CurrCol;
  61.                 if (wnd->WndRow > ClientHeight(wnd)-1)    {
  62.                     wnd->wtop = wnd->CurrLine;
  63.                     wnd->WndRow = 0;
  64.                 }
  65.                 SendMessage(wnd, PAINT, 0, 0);
  66.                 SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
  67.                 if (Replacing)    {
  68.                     if (rpl || YesNoBox("Replace the text?"))    {
  69.                         char *cr = GetEditBoxText(db, ID_REPLACEWITH);
  70.                         int oldlen = strlen(cp);
  71.                         int newlen = strlen(cr);
  72.                         int dif;
  73.                         if (oldlen < newlen)    {
  74.                             dif = newlen-oldlen;
  75.                             if (wnd->textlen < strlen(wnd->text)+dif)    {
  76.                                 int offset = (int)(cp1-wnd->text);
  77.                                 wnd->textlen += dif;
  78.                                 wnd->text = realloc(wnd->text, wnd->textlen+2);
  79.                                 if (wnd->text == NULL)
  80.                                     return;
  81.                                 cp1 = wnd->text + offset;
  82.                             }
  83.                             memmove(cp1+dif, cp1, strlen(cp1)+1);
  84.                         }
  85.                         else if (oldlen > newlen)    {
  86.                             dif = oldlen-newlen;
  87.                             memmove(cp1, cp1+dif, strlen(cp1)+1);
  88.                         }
  89.                         strncpy(cp1, cr, newlen);
  90.                         wnd->TextChanged = TRUE;
  91.                         BuildTextPointers(wnd);
  92.                     }
  93.                     if (rpl)    {
  94.                         incr = TRUE;
  95.                         continue;
  96.                     }
  97.                     ClearTextBlock(wnd);
  98.                     SendMessage(wnd, PAINT, 0, 0);
  99.                 }
  100.                 return;
  101.             }
  102.             break;
  103.         }
  104.     }
  105.     if (cp && *cp && !FoundOne)
  106.         MessageBox("Search/Replace Text", "No match found");
  107. }
  108.  
  109. void ReplaceText(WINDOW wnd)
  110. {
  111.     if (CheckCase)
  112.         SetCheckBox(&ReplaceTextDB, ID_MATCHCASE);
  113.     if (DialogBox(wnd, &ReplaceTextDB, TRUE, NULL))    {
  114.         CheckCase = CheckBoxSetting(&ReplaceTextDB, ID_MATCHCASE);
  115.         SearchTextBox(wnd, TRUE, FALSE);
  116.     }
  117. }
  118.  
  119. void SearchText(WINDOW wnd)
  120. {
  121.     if (CheckCase)
  122.         SetCheckBox(&SearchTextDB, ID_MATCHCASE);
  123.     if (DialogBox(wnd, &SearchTextDB, TRUE, NULL))    {
  124.         CheckCase = CheckBoxSetting(&SearchTextDB, ID_MATCHCASE);
  125.         SearchTextBox(wnd, FALSE, FALSE);
  126.     }
  127. }
  128.  
  129. void SearchNext(WINDOW wnd)
  130. {
  131.     SearchTextBox(wnd, FALSE, TRUE);
  132. }
  133.  
  134.  
  135.